import { action, computed, observable } from 'mobx';
import { tool,get,uuid } from '../apis/API';
import { login, writeFloor,writeLayer,writePost,getEssay,getComment,getUser } from '../utils/Urls'
import { getCookie } from '../utils/GetCookieToMap'
import md5 from 'md5';

export class Store {
    @observable status = false;
    @observable essayList = [];
    @observable userName = "";
    @observable userId = -1;
    @observable token = "";

    @computed get loginStatus() {
        return this.status;
    }
    set loginStatus(status) {
        this.status = status;
    }

    @action
    change = () => {
        this.status = !this.status;
        console.log("status:" + this.status);
    }

    @action
    changeStatus = () => {
        this.status = true;
        this.userName = getCookie("cl_name");
        this.userId = 1;
        this.token = getCookie("token");
    }

    @action
    getAllEssay = async (type) => {

        let params = `?type=${type}&index=0&size=10&eventNo=${uuid(10,10)}&token=${this.token}`;
        const res = await get(getEssay+params);
        console.log(res);
        if (res == undefined) {
            return; //服务器问题/网络问题
        }
        if(res.data.token!=undefined && res.data.token!=""){
            console.log("yes");
            this.token = res.data.token;
            document.cookie = "token="+res.data.token;
        }
    
        this.essayList = res.data.essayList;
    }

    @action
    login = async (name, password) => {
        let data = {
            "name":name,
            "password":md5(password),
            "eventNo":uuid(10,10)
        }
        const res = await tool(login, data, "POST");
        console.log(res.data.token);
        if (res != undefined) {
            if (res.code === 200) {
                this.userName = name;
                this.userId = 1;
                this.status = true;
                this.token = res.data.token;
                document.cookie = "cl_name="+name;
                document.cookie = "token="+res.data.token;
            } else {
                this.status = false;
                this.userId = -10;//输入用户名和密码不匹配
            }
        } else {
            this.status = false;
            this.userId = -100; //服务器问题/网络问题
        }
    }

    @action
    register = async (name, password,tel, email, homePage) => {
        let data = {
            "name": name,
            "password": password,
            "tel":tel,
            "email": email,
            "homePage": homePage,
            "eventNo":uuid(10,10)
        };
        const res = await tool(getUser, data, "POST");
        return res;
    }

    @action
    getEssayDetails = async (id) => {
        const res = await get(getEssay+"/"+id+`/?eventNo=${uuid(10,10)}`);
        console.log(res);
        return res;
    }

    @action
    getCommentDetails = async(type,eid) => {
        const res = await get(getComment+"/"+type+"/"+eid+`/?eventNo=${uuid(10,10)}&token=${this.token}`);
        console.log(res);
        return res;
    }

    handleWriteComment = async(content,type,belongId,repliedId) => {
        let token = this.token;
        let uuid2 = uuid(10,10);
        let data = {
            "content":content,
            "eventNo":uuid2,
            "type":type,
            "belongId":belongId,
            "repliedId":repliedId
        };
        const res = await tool(getComment+"?token="+token, data, "POST");
        return res;
    }

    @action
    handleWriteEssay = async (title,text) => {
        let token = this.token;
        let uuid2 = uuid(10,10);
        let data = {
            "title":title,
            "eventNo":uuid2,
            "type":1,
            "content":"",
            "text":text
        };
        const res = await tool(getEssay+"?token="+token, data, "POST");
        return res;
    }



    @action
    handleWriteFloor = async (e_id, content, u_id, level,info) => {
        console.log(e_id);
        let data = {
            "essay": e_id,
            "content": content,
            "publisher": u_id,
            "level": level,
            "info":info
        };
        const res = await tool(writeFloor, data, {});
        return res;
    }

    @action
    handleWriteLayer = async (f_id,content,publisher,responder,level,replied_lid) => {
        let data = {
            "floor": f_id, 
            "content": content,
            "publisher": publisher,
            "responder": responder,
            "level": level, 
            "replied_lid": replied_lid 
        };
        const res = await tool(writeLayer, data, {});
        return res;
    }

    @action
    handleWritePost = async (title, content, u_id,label,type,info) => {
        console.log(u_id);
        let data = {
            "title":title,
            "content":content,
            "publisher":u_id,   //这个是发布人，楼主id
            "label":label,  //发表内容归类，标签
            "type":type,  /* 文章类型，分为：BLOG/ESSAY/DIARY 其中BLOG为新闻、资讯、技术等分享类型文章，由系统管理员直接发布，原则上不允许用户进行发布；ESSAY为默认类型，属于圈子，由用户发布并永久属于PULIC类型的文章；DIARY为动态类型，由用户发布，可以自定义隐私：PUBLIC(公开)/PRIVATE(私密)/ONLYFANS(仅关注之人) */
            "info":info //前端自定义json字段，自己定义自己解析
        };
        const res = await tool(writePost, data, {});
        return res;
    }

}